package com.immediatus.graphics.buffer;

import com.immediatus.graphics.font.Font;
import com.immediatus.util.FastFloatBuffer;

public class TextTextureBuffer extends BufferObject{

    public TextTextureBuffer(final int capacity_, final int type_, final boolean managed_){
        super(capacity_, type_, managed_);
    }

    public synchronized void update(final Font font_, final String[] lines_){
        final FastFloatBuffer textureFloatBuffer = this.getFloatBuffer();
        textureFloatBuffer.position(0);

        final Font font = font_;
        final String[] lines = lines_;

        final int lineCount = lines.length;
        for (int i = 0; i < lineCount; i++){
            final String line = lines[i];

            final int lineLength = line.length();
            for (int j = 0; j < lineLength; j++){
                final com.immediatus.graphics.font.Character ch = font.getLetter(line.charAt(j));

                final float letterTextureX = ch.getTextureX();
                final float letterTextureY = ch.getTextureY();
                final float letterTextureX2 = letterTextureX + ch.getTextureWidth();
                final float letterTextureY2 = letterTextureY + ch.getTextureHeight();

                textureFloatBuffer.put(letterTextureX);
                textureFloatBuffer.put(letterTextureY);

                textureFloatBuffer.put(letterTextureX);
                textureFloatBuffer.put(letterTextureY2);

                textureFloatBuffer.put(letterTextureX2);
                textureFloatBuffer.put(letterTextureY2);

                textureFloatBuffer.put(letterTextureX2);
                textureFloatBuffer.put(letterTextureY2);

                textureFloatBuffer.put(letterTextureX2);
                textureFloatBuffer.put(letterTextureY);

                textureFloatBuffer.put(letterTextureX);
                textureFloatBuffer.put(letterTextureY);
            }
        }
        textureFloatBuffer.position(0);

        this.setHardwareBufferNeedsUpdate();
    }
}
